<template>
  <van-nav-bar title="修改密码" left-text="返回" left-arrow @click-left="onClickLeft" />

  <div>
    <van-cell-group inset>
      <van-field v-model="oldPassword" label="旧密码" placeholder="请输入旧密码" />
      <van-field v-model="newPassword" label="新密码" placeholder="请选择新密码" />
      <van-field v-model="rNewPassword" label="确认密码" placeholder="请确认密码" />
    </van-cell-group>
    <van-button type="primary" class="sure" @click="yes()">保存</van-button>
  </div>
</template>
<script>
import { inforeq, rpwdreq } from '../../api/requies'

export default {
  methods: {
    onClickLeft() {
      this.$router.back()
    },
    yes() {
      const id = localStorage.getItem('id')
      rpwdreq(id, this.oldPassword, this.newPassword, this.rNewPassword).then((r) => {
        console.log(r.data)
        if (r.data.code == 1) {
          // eslint-disable-next-line no-undef
          showSuccessToast(r.data.message)
          localStorage.removeItem('id')
          localStorage.removeItem('token')
          this.$router.push('/login')
        } else {
          // eslint-disable-next-line no-undef
          showFailToast(r.data.message)
        }
      })
    },
    loadlist() {
      const id = localStorage.getItem('id')
      inforeq(id).then((r) => {
        console.log(r.data)
        this.user = r.data.info
      })
    }
  },
  data() {
    return {
      user: {},
      oldPassword: '',
      newPassword: '',
      rNewPassword: this.newPassword
    }
  },
  mounted() {
    this.loadlist()
  }
}
</script>
<style lang="scss">
.sure {
  width: 100px;
  position: absolute;
  left: 38%;
  top: 60%;
}
</style>
